package stage_1_module_4.code.homework_3;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 拷贝文件的线程
 */
public class Copy extends Thread {
    private String sfile;
    private String tfile;

    @Override
    public void run() {
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            bis = new BufferedInputStream(new FileInputStream(sfile));
            bos = new BufferedOutputStream(new FileOutputStream(tfile));

            byte[] bt = new byte[1024];
            int res = 0;
            while ((res = bis.read(bt)) != -1) {
                bos.write(bt,0,res);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if (null != bis) {
                try {
                    bis.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if (null !=bos) {
                try {
                    bos.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            System.out.println("创建文件:"+tfile);

        }
    }

    public Copy(String sfile, String tfile) {
        this.sfile = sfile;
        this.tfile = tfile;
    }
}
